import { defineConfig } from 'vite'
import uni from '@dcloudio/vite-plugin-uni'
import { resolve} from 'path'

const pathResolve = (dir) => resolve(__dirname, '.', dir)

// 时间戳
const timestamp = new Date().getTime();

// 代理地址
const targetUrl = 'http://66.42.48.200:9004/'
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    uni(),
  ],
  server: {
    proxy: {
      '/api':{
          target: targetUrl,
          changeOrigin:true,
          rewrite: (path) => path.replace(/^\/api/, '')
        },
    },
  },
  resolve:{
    alias: [
      {
        find: '@',
        replacement: pathResolve('src')
      },
    ],
  },
  build: {
    rollupOptions: {
      output: {
          entryFileNames: ({ name }) => {
            if (name === 'vendor') {
              return `assets/js/[name].js`; // 第三方库不添加时间戳
            } else {
              return `assets/js/[name]-[hash]-${timestamp}.js`; // 自定义文件名，不使用哈希值
            }
          },
          chunkFileNames: `assets/js/[name]-[hash].js`, // 自定义文件名，不使用哈希值
          assetFileNames: `assets/[ext]/[name]-[hash].[ext]` // 资源文件添加时间戳
        },
      }
    }
})
